/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Rewriting;

import SPARQL.SPARQL;
import SPARQL.Utilities.QueryRenamer;
import SPARQL.Variable;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author mirko
 */
public class SPARQLEquivalenceChecker {
    
    
    
    public boolean areEquivalent(SPARQL query1, SPARQL query2){
        
        Boolean equivalent = false;
        if (query1.sizeOfVariables()!= query2.sizeOfVariables())
            return equivalent;
        if (query1.getTriplePatterns().size()!=query2.getTriplePatterns().size())
            return equivalent;
        List<Variable> listvar1, listvar2;
        listvar1 = new LinkedList<Variable>(query1.getSetOfAllVariables());
        listvar2 = new LinkedList<Variable>(query2.getSetOfAllVariables());
        
        PermutationsGenerator<Variable> permgen = new PermutationsGenerator<Variable>();
        List<List<Variable>> setAllOrdersList2 = permgen.generatePerm(listvar2);
        
        for (List<Variable> listvarSort2 : setAllOrdersList2){
            Map<Variable, Variable> map = new HashMap<Variable, Variable>();
            for (int i=0; i < listvar2.size(); i++){
                
                map.put(listvar1.get(i), listvarSort2.get(i));
                
            
        }
            QueryRenamer renamer = new QueryRenamer(query1);
            renamer.renameVariables(map);
            if (renamer.getQuery().equals(query2))
                return true;
            
        }
                
        
        
        
        return equivalent;   
    }
    
    
    
    
    
    
    
}
